package com.donfu.web.controller.wxhouse;

import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.core.page.TableDataInfo;
import com.donfu.common.enums.BusinessType;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.system.domain.house.*;
import com.donfu.system.service.house.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 收藏Controller
 * 
 * @author 曹雨心
 * @date 2021-12-01
 */
@RestController
@RequestMapping("/wx/house/collection")
public class WebHouseCollectionController extends BaseController
{
    @Autowired
    private IHouseCollectionService houseCollectionService;
    @Autowired
    private IHouseGoodsService houseGoodsService;
    @Autowired
    private IHouseCommentService houseCommentService;
    @Autowired
    private IHouseCommentLikeService houseCommentLikeService;


    /**
     * 根据用户id 查询收藏列表
     */
   // @PreAuthorize("@ss.hasPermi('house:collection:list')")
    @GetMapping("/list")
    public TableDataInfo userList(HouseCollection houseCollection)
    {
        startPage();
        List<Map<String,Object>> listMap = new ArrayList<>();

        List<HouseCollection> list = houseCollectionService.selectHouseCollectionList(houseCollection);
        for (HouseCollection collection : list) {
            Map<String, Object> map = new HashMap<>();
            if (collection.getType()==1) {
                //产品收藏
                HouseGoods goods = houseGoodsService.selectHouseGoodsById(collection.getValueId());
                map.put("goods",goods);
            }else if (collection.getType()==2){
                //客人的家收藏
                HouseComment houseComment = houseCommentService.selectHouseCommentById(collection.getValueId());
                houseComment.setCollStatus(new Long(1));
                HouseCommentLike houseCommentLike = new HouseCommentLike();
                //查询点赞状态
                houseCommentLike.setUserId(collection.getUserId().intValue());
                houseCommentLike.setCommentId(collection.getValueId().intValue());
                houseCommentLike.setStatus(1);
                List<HouseCommentLike> houseCommentLikes = houseCommentLikeService.selectHouseCommentLikeList(houseCommentLike);
                if (houseCommentLikes.size() > 0) {
                    //该用户已点赞
                    houseComment.setLikeStatus(new Long(1));
                } else {
                    //该用户未点赞
                    houseComment.setLikeStatus(new Long(0));
                }
                map.put("comment",houseComment);
            }
            listMap.add(map);
        }

        return getDataTable(listMap);
    }

    /**
     * 新增收藏
     */
    //@PreAuthorize("@ss.hasPermi('house:collection:add')")
//    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseCollection houseCollection)
    {
        houseCollection.setCreateDate(new Date());
        return toAjax(houseCollectionService.insertHouseCollection(houseCollection));
    }

    /**
     * 删除收藏
     */
    //@PreAuthorize("@ss.hasPermi('house:collection:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseCollectionService.deleteHouseCollectionByIds(ids));
    }
}
